﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SailHero.Soft.FrameWork.Models;
using UI.Web.SL.Demo.RIAServices.Web;

namespace UI.Web.Silverlight.Demo.Views
{
    public partial class AddCustomerView : UserControl
    {
        public AddCustomerView()
        {
            InitializeComponent();
        }

        private CustomerDContext _customerDContext = new CustomerDContext();
        private CUSTOMER _customer;

        private void AddCustomer()
        {
            this.Customer = new CUSTOMER() { AGE = Convert.ToDecimal(this.textBox2.Text), NAME = this.textBox1.Text };
            //CUSTOMER custm = new CUSTOMER() {AGE = 20, NAME = "admin", ID = 1222};
            _customerDContext.CUSTOMERs.Add(this.Customer);
            //_customerDContext.CUSTOMERs.Add(custm);
            _customerDContext.SubmitChanges(AddCustomerCompleted, null);
        }
        public CUSTOMER Customer
        {
            get { return _customer; }
            set
            {
                _customer = value;
            }
        }


        private void AddCustomerCompleted(SubmitOperation operation)
        {
            if (operation.IsComplete)
            {

                this.Customer = new CUSTOMER();
                if(WorkBench.AddedCustomer!=null)
                {
                    WorkBench.AddedCustomer.DynamicInvoke();
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            AddCustomer();
        }

    }
}
